package com.banciyuan.bcywebview.biz.detail.post;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.groupdetail.NewTeamDetailActivity;
import com.banciyuan.bcywebview.biz.web.WebActivity;
import com.banciyuan.bcywebview.biz.write.photoselecotor.util.CommonUtils;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.regex.MatchFilterImpl;
import com.banciyuan.bcywebview.utils.regex.MyLinkfy;
import com.banciyuan.bcywebview.utils.regex.MyRegex;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.string.UnderLineUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Complex;

/**
 * Created by squall on 2016/8/16.
 */
public class PostHeaderHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private TextView tv_groupName;
    private LinearLayout lock_container;
    private TextView lock_reason;
    private LinearLayout viewCounts_ll;
    public TextView focus_tv;
    private TextView viewCounts_tv;
    private TextView name_tv;
    private TextView time_tv;
    private ImageView circle_iv;
    private TextView content_tv;
    private TextView intro_tv;
    private TextView lock_more;

    private Context mContext;
    private Complex complex;

    public PostHeaderHolder(View itemView) {
        super(itemView);
        viewCounts_ll = (LinearLayout) itemView.findViewById(R.id.viewcount);
        circle_iv = (ImageView) itemView.findViewById(R.id.civ_header_top);
        focus_tv = (TextView) itemView.findViewById(R.id.tv_recommended_top);
        viewCounts_tv = (TextView) itemView.findViewById(R.id.tv_viewcount);
        name_tv = (TextView) itemView.findViewById(R.id.tv_name_top);
        time_tv = (TextView) itemView.findViewById(R.id.tv_time_top);
        intro_tv = (TextView) itemView.findViewById(R.id.tv_intro_top);

        tv_groupName = (TextView) itemView.findViewById(R.id.topic_name);
        lock_container = (LinearLayout) itemView.findViewById(R.id.lock_container);
        lock_reason = (TextView) itemView.findViewById(R.id.lock_reason);
        content_tv = (TextView) itemView.findViewById(R.id.tv_group_content);
        lock_more = (TextView) itemView.findViewById(R.id.tv_post_lock_link);

        name_tv.setOnClickListener(this);
        time_tv.setOnClickListener(this);
        circle_iv.setOnClickListener(this);
        intro_tv.setOnClickListener(this);

        focus_tv.setOnClickListener(this);
        tv_groupName.setOnClickListener(this);
        lock_more.setOnClickListener(this);
    }

    public void setData(Complex complex, Context mContext) {
        this.mContext = mContext;
        this.complex = complex;

        int maxNameWidth = CommonUtils.getWidthPixels((Activity) mContext) - UIUtils.dip2px(208, mContext);
        name_tv.setMaxWidth(maxNameWidth);
        if (complex.getStatus() == 1) {
            name_tv.setText(complex.getProfile().getUname());
            if (!TextUtils.isEmpty(complex.getCtime()))
                time_tv.setText(StringUtil.getOppositeTime(complex.getCtime()));
            if (TextUtils.isEmpty(complex.getProfile().getSelf_intro())) {
                intro_tv.setVisibility(View.GONE);
            } else {
                intro_tv.setVisibility(View.VISIBLE);
                intro_tv.setText(Html.fromHtml(complex.getProfile().getSelf_intro()));
            }
            ImageLoader.getInstance().displayImage(complex.getProfile().getAvatar(), circle_iv, ImageOptions.optionHead);
            if (TextUtils.isEmpty(complex.getPlain())) {
                content_tv.setVisibility(View.GONE);
            } else {
                content_tv.setVisibility(View.VISIBLE);
                content_tv.setMovementMethod(LinkMovementMethod.getInstance());
                SpannableString spannableString = StringUtil.setAtUser(StringUtil.htmlDecoder(complex.getPlain()), complex.getAt_users(), mContext);
                MyLinkfy.addLinks(spannableString, MyRegex.plist.get(0), "bcyapp_", new MatchFilterImpl(), null);
                content_tv.setText(spannableString);
                UnderLineUtil.DisUnderLine(mContext, content_tv);
            }
            if (complex.getProfile().getUid().equals(UserDataHelper.getInstance(mContext).getUid())) {
                viewCounts_ll.setVisibility(View.VISIBLE);
                focus_tv.setVisibility(View.INVISIBLE);
                viewCounts_tv.setText(complex.getView_count());
            } else {
                if (complex.getProfile().getFollowstate().equals("unfollow")) {
                    focus_tv.setVisibility(View.VISIBLE);
                } else {
                    focus_tv.setVisibility(View.GONE);
                }
            }
            if (complex.getGroup() != null && !TextUtils.isEmpty(complex.getGroup().getName())) {
                tv_groupName.setText(Html.fromHtml(complex.getGroup().getName()));
            }
        } else if (complex.getStatus() == MyConstants.WORK_LOKED) {
            lock_container.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_name_top:
            case R.id.tv_intro_top:
            case R.id.tv_time_top:
            case R.id.civ_header_top:
                BaseObserver.getInstance().notifyWatchers(BaseObserver.GO_PERSON);
                break;
            case R.id.tv_recommended_top:
                TimelineLikeHelper.doFollow(mContext, "dofollow", complex.getProfile().getUid(), new TimelineLikeHelper.FollowSuccess() {
                    @Override
                    public void onFollowSuccess(String response) {
                        if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                            MyToast.show(mContext, mContext.getString(R.string.focus_succ));
                            BaseObserver.getInstance().notifyWatchers(BaseObserver.FOLLOW_EVENT);
                        }
                    }
                });
                break;
            case R.id.topic_name:
                gotoUtil.gotoAct(mContext, NewTeamDetailActivity.class, complex.getGid());
                break;
            case R.id.tv_post_lock_link:
                gotoUtil.gotoAct(mContext, WebActivity.class, mContext.getString(R.string.lock_link));
                break;
        }
    }
}
